package marmalius;


class Fishing implements Runnable {
    Thread t;
    String fish;
    int fishId;
    int lvlreq;
    int anim;
    int itemreq;
    int playerId;
    int xp;
	
    Fishing(int playerId, String fish, int fishId, int lvlreq, int anim, int itemreq, int xp) {
        this.playerId = playerId;
        this.fish = fish;
        this.fishId = fishId;
        this.xp = xp;
        this.lvlreq = lvlreq;
        this.anim = anim;
        this.itemreq = itemreq;
        t = new Thread(this, "fish");
        t.start();
    }
    public int origwep;
    public int xprate = 1;
    public void run() {
        client c = (client) PlayerHandler.getInstance().getPlayer(playerId);

        if (c.playerHasItemAmount(itemreq, 1)) {
            if (c.playerLevel[10] > lvlreq) {
                c.startAnimation(anim);
                c.sendMessage("You start fishing for " + fish + "s.");
                c.isfishing = true;
                startfish();
            } else {
                c.sendMessage(
                        "You need a higher fishing level of " + lvlreq
                        + " to catch " + fish + "s.");
            }
        } else {
            c.sendMessage(
                    "You need a " + c.getItemName(itemreq) + " to catch " + fish
                    + "s.");
        }
    }

    public void startfish() {
        try {
            client c = (client) PlayerHandler.getInstance().getPlayer(playerId);

            try {
                Thread.sleep(
                        misc.random(15000)
                                + misc.random(30000 - (c.playerLevel[10] * 250)));
                c.startAnimation(anim);
            } catch (InterruptedException e) {
                c.isfishing = false;
            }
            if (c.freeSlots() > 1 && c.isfishing) {				
                c.sendMessage("You caught a " + fish + ".");
                c.addSkillXP(xp, 10);
                c.addItem(fishId, 1);
                c.startAnimation(anim);
                startfish();
            } else if (c.freeSlots() == 1 && c.isfishing) {
                c.sendMessage("you catched a " + fish + ".");
                c.addSkillXP(xprate * lvlreq, 10);
                c.addItem(fishId, 1);
                c.sendMessage("Not enough space in inventory.");
                c.isfishing = false;
            } else if (!c.isfishing) {} else {
                c.sendMessage("[ERROR]: unable to catch any fish.");
                c.sendMessage(
                        "[ERROR]: try again later, if it still doesnt work");
                c.sendMessage("[ERROR]: please post on the forums.");
            }
        } catch (Exception E) {}
    }

}
